package com.jxt.myjxtmanager.ui.activity;

import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.inqbarna.tablefixheaders.TableFixHeaders;
import com.jxt.myjxtmanager.BaseActivity;
import com.jxt.myjxtmanager.R;
import com.jxt.myjxtmanager.adapter.SearchTypeAdapter;
import com.jxt.myjxtmanager.db.Student;
import com.jxt.myjxtmanager.entity.SearchTypeBean;
import com.jxt.myjxtmanager.ui.inter.CheckWorkInter;
import com.jxt.myjxtmanager.ui.presenter.CheckWorkPresenter;
import com.jxt.myjxtmanager.ui.tableadapter.FamilyNexusAdapter;
import com.jxt.myjxtmanager.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * 考勤
 * Created by qm on 2017/2/10 0010.
 */
public class CheckWorkActivity extends BaseActivity implements CheckWorkInter, AdapterView.OnItemClickListener {

    private CheckWorkPresenter presenter;
    private TableFixHeaders tableFixHeaders;
    private FamilyNexusAdapter baseTableAdapter;
    private List<Student> dataList;
    private TextView tv_pop_isshow;
    private LinearLayout lin_triangle;
    private TextView mTv_select_sn;
    private TextView tv_check_num;

    private List<SearchTypeBean> list = new ArrayList<>();
    private PopupWindow popupWindow;
    private SearchTypeAdapter adapter;

    /**
     * 获取数据成功后回调
     */
    @Override
    public void setMyAdapter() {
        presenter.dismissDialog();
        if (!TextUtils.isEmpty(presenter.getLateNum()))
            tv_check_num.setText("三年级六班—"+"缺勤人数：" + presenter.getLateNum() + " 人");
        dataList = presenter.getDataList();//获取数据
        if (baseTableAdapter == null) {//设置适配器adapter  如果为空就 new 出  否则则刷新数据就行
            baseTableAdapter = new FamilyNexusAdapter(this, this, presenter);
//        baseTableAdapter.setDeviceOutLine(presenter.getDeviceOutLine());
            baseTableAdapter.setDevicesDates(dataList);
            tableFixHeaders.setAdapter(baseTableAdapter);
        } else {
            baseTableAdapter.setDevicesDates(dataList);
            baseTableAdapter.notifyDataSetChanged();
        }
    }

    /**
     * 选择日期后获取数据成功后回调
     */
    @Override
    public void updateRecycler() {
        dataList = presenter.getDataList();
        baseTableAdapter.setDevicesDates(dataList);
        baseTableAdapter.notifyDataSetChanged();

    }

    @Override
    public void showToastMsg(String msg) {
        if (!TextUtils.isEmpty(msg)) {
            ToastUtil.showToast(this, msg);
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_excel_layout;
    }

    @Override
    protected void initView() {
        setToolbarTitle("考勤");
        setToolbarNavigationIcon(R.drawable.ic_left_return);
        tableFixHeaders = (TableFixHeaders) findViewById(R.id.table);
        mTv_select_sn = (TextView) findViewById(R.id.tv_select_sn);
        lin_triangle = (LinearLayout) findViewById(R.id.lin_triangle);
        tv_pop_isshow = (TextView) findViewById(R.id.tv_pop_isshow);
        tv_check_num = (TextView) findViewById(R.id.tv_check_num);

        mTv_select_sn.setOnClickListener(this);
    }

    @Override
    public void onClickLeftMenu() {
        super.onClickLeftMenu();
    }

    @Override
    protected void initData() {
        list.add(new SearchTypeBean("今天"));
        list.add(new SearchTypeBean("昨天"));
        list.add(new SearchTypeBean("前天"));
        presenter = new CheckWorkPresenter(this, this);
        presenter.RequestData();
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.tv_select_sn:
                //弹出popwindow
                getPullDownList();

                break;
        }
    }


    /**
     * 自定义PopWindow
     */
    public void getPullDownList() {
        popupWindow = new PopupWindow(this);
        View view = LayoutInflater.from(this).inflate(R.layout.popup_layout, null);
        ListView leftLV = (ListView) view.findViewById(R.id.pop_listview_left);
        popupWindow.setContentView(view);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));//PaintDrawable
        popupWindow.setFocusable(true);
        popupWindow.showAsDropDown(mTv_select_sn);
        adapter = new SearchTypeAdapter(this, list);
        leftLV.setAdapter(adapter);
        leftLV.setOnItemClickListener(this);
        tv_pop_isshow.setVisibility(View.VISIBLE);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                tv_pop_isshow.setVisibility(View.GONE);
            }
        });
    }

    /**
     * popWindow adapter item点击事件
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        SearchTypeBean mBean = (SearchTypeBean) adapter.getItem(position);
        String type_name = mBean.getType_name();
        mTv_select_sn.setText(type_name);
        if (popupWindow.isShowing()) {
            popupWindow.dismiss();//dismiss掉当前显示的popWindow
        }
        presenter.RequestData();//刷新界面数据
    }
}
